from flask import Flask, render_template
from pyecharts import charts, options
import pandas as pd

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/index2')
def index2():
    return render_template('index2.html')


@app.route('/project', methods=['GET'])
def project():
    return render_template('mpg.html')


@app.route('/city', methods=['GET'])
def city():
    data = pd.read_excel('jobs.xlsx')
    city_chart = charts.Bar(init_opts=options.InitOpts(width='1000px', height='600px'))
    city_pt = data.pivot_table(index='city', values='salary', aggfunc='mean')
    city_pt = city_pt.sort_values('salary',
                                  ascending=False)
    city_chart.add_xaxis(list(city_pt.index.values))
    city_chart.add_yaxis('', [round(float(x)) for x in list(city_pt['salary'])],
                         label_opts=options.LabelOpts(is_show=False)).set_global_opts(
        datazoom_opts=options.DataZoomOpts(range_start=0, range_end=10, is_show=True))
    city_chart.set_global_opts(title_opts=options.TitleOpts('各城市平均薪资',
                                                            subtitle='海外地区的招聘职位薪资最高，比国内城市中最高的北京还要高出一万多，一线城市平均薪资可达2万以上，其余各新一线城市也在1-2万之间'))
    return city_chart.render_embed()


@app.route('/map', methods=['GET'])
def job_map():
    city_data = pd.read_excel('jobs.xlsx')
    city_pt = city_data.pivot_table(index='city', values='salary', aggfunc='count').sort_values('salary',
                                                                                                ascending=False)
    city_map = charts.Map()
    city_map.add('', [(x, round(float(city_pt['salary'][x]))) for x in city_pt.index.values],
                 maptype='china-cities',
                 label_opts=options.LabelOpts(is_show=False)).set_global_opts(
        visualmap_opts=options.VisualMapOpts(min_=min(city_pt['salary']), max_=max(city_pt['salary'])))
    city_map.set_global_opts(title_opts=options.TitleOpts('全国区域分布图',
                                                          subtitle='计算机和大数据相关职位主要分布在京津冀、长三角、珠三角地区'))
    return city_map.render_embed()


@app.route("/exp", methods=['GET'])
def exp():
    data_kw = pd.read_excel('jobs.xlsx')
    year = ['不限', '在校/应届', '1年以下', '1-3年', '3-5年', '5-10年', '10年以上']
    exp_chart = charts.Boxplot()
    exp_chart.add_xaxis(year)
    exp_chart.add_yaxis('',
                        exp_chart.prepare_data([list(data_kw['salary'].loc[data_kw['workYear'] == x]) for x in year]))
    exp_chart.set_global_opts(title_opts=options.TitleOpts('不同工作经验对应平均薪资',
                                                           subtitle='经验与薪资密切相关，5年以上的薪资中位数已达3万，最高接近10万；'
                                                                    '应届生薪资中位数超过13500，最高可达3万以上'))
    return exp_chart.render_embed()


@app.route("/edu", methods=['GET'])
def edu():
    data_kw = pd.read_excel('jobs.xlsx')
    education = ['不限', '大专', '本科', '硕士', '博士']
    edu_pt = data_kw.pivot_table(index='education', values='salary', aggfunc='count')
    edu_bar = charts.Pie()
    edu_bar.add('',
                [(x, round(float(edu_pt['salary'][x]))) if x in edu_pt.index.values else None for x in education],
                label_opts=options.LabelOpts(formatter='{b},{d}%'))
    edu_bar.set_global_opts(
        title_opts=options.TitleOpts('不同学历要求职位数量占比',
                                     subtitle='在约5000条招聘信息中，约87%的职位要求学历在本科及以上，本科以下学历仅10%'),
        legend_opts=options.LegendOpts(pos_bottom='0'))
    edu_bar.render()
    return edu_bar.render_embed()


@app.route('/wc', methods=['GET'])
def wc():
    data = pd.read_excel('jobs.xlsx')
    data['skillLables'] = data['skillLables'].apply(lambda x: x.split(',') if not pd.isna(x) else x)
    data = data.explode('skillLables')
    labels = data.pivot_table(index='skillLables', values='positionId', aggfunc='count')
    labels = labels.sort_values('positionId', ascending=False)
    labels_chart2 = charts.WordCloud()
    labels_chart2.add('', [(x[0], int(x[1]['positionId'])) for x in labels.iterrows()])
    labels_chart2.set_global_opts(title_opts=options.TitleOpts('招聘信息技能要求词云图',
                                                               subtitle='java、android、C++依然是最热门的技能标签，python、数仓也有很高热度'))
    print([(x[0], x[1]['positionId']) for x in labels.iterrows()])
    labels_chart2.render('wc.html')
    return labels_chart2.render_embed()


@app.route('/skill', methods=['GET'])
def skill():
    data = pd.read_excel('jobs.xlsx')
    data['skillLables'] = data['skillLables'].apply(lambda x: x.split(',') if not pd.isna(x) else x)
    data = data.explode('skillLables')
    labels = data.pivot_table(index='skillLables', values='positionId', aggfunc='count')
    labels = labels.sort_values('positionId', ascending=False).head(20)
    labels_chart1 = charts.Bar()
    labels_chart1.add_xaxis(list(labels.index.values))
    labels_chart1.add_yaxis('', list(labels['positionId']), color='#87CEEB').set_global_opts(
        datazoom_opts=options.DataZoomOpts(range_start=0, range_end=30))
    labels_chart1.set_global_opts(title_opts=options.TitleOpts('各技能标签招聘职位数量',
                                                               subtitle='在所有技能标签中，java高居榜首，其次是C++，ios和安卓紧随其后'))
    return labels_chart1.render_embed()


@app.route('/mpg', methods=['GET'])
def mpg_view():
    return render_template('mpg.html')


if __name__ == '__main__':
    app.run('0.0.0.0', port=88, debug=True)
